package hc.bre

import static org.junit.jupiter.api.Assertions.*

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

import hc.Util
import hc.bre.flow.FlowDefinition
import hc.bre.flow.FlowLineItem
import hc.bre.flow.FlowNodeItem

@SpringBootTest
@groovy.transform.CompileStatic
class FlowTest {

	@Test
	void newObject() {
		new FlowNodeItem();
		new FlowLineItem();
		new FlowDefinition();
	}
	@Test
	void create() {
		FlowDefinition ruleDef =  FlowDefinition.create();
		assert ruleDef.nodeList.size() == 2;
		assert ruleDef.nodeList[0]["itemType"] == 'start';
		assert ruleDef.nodeList[1]["itemType"] == 'end';
		assert ruleDef.lineList.size() == 1;
	}

	@Test
	void createAndBuild() {
		FlowDefinition ruleDef =  FlowDefinition.create();
		ruleDef.rname='决策流1';
		ruleDef.rpath ='test/Flow1';
		ruleDef.buildRuleClass(null).getName() == 'test.Flow1';
	}

	@Test
	void createAndEvaluate() {
		FlowDefinition ruleDef =  FlowDefinition.create();
		ruleDef.rname='决策流1';
		ruleDef.rpath ='test/Flow1';
		def ret = ruleDef.buildRuleEvaluate([:],null) as Map;
		assert ret.result!=null;
	}

	String jsonText =
	'''
{
    "rname": "决策流1",
    "rversion": 3,
    "rmemo": "",
    "nodeList": [
        {
            "itemType": "start",
            "id": 1,
            "positionTop": 30,
            "positionLeft": 250
        },
        {
            "itemType": "end",
            "id": 2,
            "positionTop": 230,
            "positionLeft": 250
        }
    ],
    "lineList": [
        {	
			"id":3,
            "sourceId": 1,
            "targetId": 2
        }
    ],
    "rid": 198,
    "rtype": "rule:flow",
    "rpath": "xsxdjc/flow/JueCeLiu1",
    "rtdata": "",
	"rmaxid":3
}
'''

	@Test
	void load() {
		Map json = Util.jsonRead(jsonText) as Map;
		def ruleDef = new FlowDefinition().load(json);
//		assert ruleDef.rid == 198
		assert ruleDef.rtype == 'rule:flow'
		assert ruleDef.rname == '决策流1'
		assert ruleDef.rpath == 'xsxdjc/flow/JueCeLiu1'
		assert ruleDef.rmaxid == 3

		assert ruleDef.nodeList.size() == 2;
		assert ruleDef.nodeList[0]["itemType"] == 'start';
		assert ruleDef.nodeList[0]["id"] == 1;
		assert ruleDef.nodeList[0]["positionTop"] == 30;
		assert ruleDef.nodeList[0]["positionLeft"] == 250;

		assert ruleDef.lineList.size() == 1;
	}

	@Test
	void buildAndEvaluate() {
		Map json = Util.jsonRead(jsonText) as Map;
		def ruleDef = new FlowDefinition().load(json);

		assert ruleDef.buildRuleClass(null).getName() =='xsxdjc.flow.JueCeLiu1'

		def ret = ruleDef.buildRuleEvaluate([:],null) as Map

		assert ret.result != null
	}

	String jsonText2='''
{
  "rid" : 309,
  "rtype" : "rule:flow",
  "rname" : "决策流2",
  "rpath" : "xsxdjc/JueCeLiu2",
  "rversion" : 3,
  "rmemo" : "",
  "rtest" : "{}",
  "rmaxid" : 10,
  "nodeList" : [ {
    "id" : 1,
    "referId" : null,
    "referName" : "结束",
    "itemType" : "start",
    "resultMap" : { },
    "positionLeft" : 500,
    "positionTop" : 100
  }, {
    "id" : 2,
    "referId" : null,
    "referName" : null,
    "itemType" : "end",
    "resultMap" : { },
    "positionLeft" : 493,
    "positionTop" : 427
  }, {
    "id" : 9,
    "referId" : 15,
    "referName" : "年龄风险分类",
    "itemType" : "task",
    "resultMap" : { },
    "positionLeft" : 468,
    "positionTop" : 262
  } ],
  "lineList" : [ {
    "id" : 10,
    "referId" : null,
    "referName" : null,
    "sourceId" : 9,
    "targetId" : 2
  }, {
    "id" : 3,
    "referId" : null,
    "referName" : null,
    "sourceId" : 1,
    "targetId" : 9
  } ]
}
'''

//	@Test TEMPXYZ
	void buildAndEvaluate2() {
		Map json = Util.jsonRead(jsonText2) as Map;
		def ruleDef = new FlowDefinition().load(json);

		assert ruleDef.buildRuleClass(null).getName() =='xsxdjc.JueCeLiu2'

		def ret = ruleDef.buildRuleEvaluate([基本信息:[年龄:40]],null)

		assert ret['result']['rname'] =="决策流2"
	}
}
